package com.behelpful.common.integration.iredis.support;

import com.behelpful.common.core.enums.IRedisCacheEnum;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

/**
 * @author Alay
 * @date 2021-09-05 18:40
 * @since
 */
@Service
@AllArgsConstructor
public class IRedisService {

    @Autowired
    private IRedisUtil iRedisUtil;


    /**
     * 通过Key删除数据
     *
     * @param key
     * @return
     */
    public Long delAsKey(String... key) {
        Jedis jedis = null;
        long del = 0;
        try {
            jedis = iRedisUtil.getJedis();
            del = jedis.del(key);
        } finally {
            iRedisUtil.close(jedis);
        }
        return del;
    }

    /**
     * 指定字段删除Hash中的数据
     *
     * @param cacheEnum
     * @param fields
     */
    @Async
    public void delHash(IRedisCacheEnum cacheEnum, String... fields) {
        Jedis jedis = null;
        try {
            jedis = iRedisUtil.getJedis();
            jedis.hdel(cacheEnum.getKey(), fields);
        } finally {
            iRedisUtil.close(jedis);
        }
    }

}
